#!/usr/bin/env python

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

class PythonJS(QObject):
    
    __pyqtSignals__ = ("contentChanged(const QString &)")
    sender = ""
    signalsEmited = 0

    @pyqtSignature("QString")
    def alert(self, msg):
        self.emit(SIGNAL('contentChanged(const QString &)'), msg)

    @pyqtSignature("int", result="QString")
    def message(self,m):
        web.page().mainFrame().evaluateJavaScript("alert('hello')")

        doc = web.page().mainFrame().documentElement()
        print doc.toInnerXml()
        doc.findFirst("div.content").setInnerXml("Hello Web Kit!")

        print m*10
        return str(m*10)

    @pyqtSignature("str", result="QString")
    def message1(self,m):
        return str(m*3)

    def register_signal(self):
        print "register_signal"
        self.signal = SIGNAL("slot(int val)")

    def slotThatEmitsSignal(self):        
        print "slotThatEmitsSignal"
        self.sender = "PythonJS"
        self.signalsEmited += 1
        self.emit(self.signal, 1)

app = QApplication(sys.argv)

web = QWebView()
# web.load(QUrl("http://192.168.27.30:3000/projects/pnp/issues"))

pjs = PythonJS()
web.page().mainFrame().addToJavaScriptWindowObject("python", pjs)
# QObject.connect(pjs, SIGNAL("contentChanged(const QString &)"),web.showMessage)

web.load(QUrl(":html/index.html"))
web.show()

sys.exit(app.exec_())
